package com.tencreat.common.utils;

import com.tencreat.common.enums.account.CitySmsTypeEnum;
import org.apache.commons.lang3.RandomStringUtils;

import java.math.BigDecimal;

/**
 * @author tang jun liang
 * @version 1.0
 * @date 2021/9/6 15:40
 */
public class TenCreatUtil {

    public static String getOrderNo() {
        String time = DateUtils.dateTime();
        String randomString = RandomStringUtils.randomNumeric(8);
        return time + randomString;
    }

    /**
     * 随机数字字串获取
     **/
    public static String getRandomNumCode(int length) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append((int) (Math.random() * 10));
        }
        return sb.toString();
    }

    public static <T extends Enum<T>> T getEnumFromString(Class<T> c, String string) {
        if (c != null && string != null) {
            try {
                return Enum.valueOf(c, string.trim().toUpperCase());
            } catch (IllegalArgumentException ex) {
            }
        }
        return null;
    }

    public static boolean isBetween(BigDecimal var, BigDecimal start, BigDecimal end) {
        return var.compareTo(start) >= 0 && var.compareTo(end) <= 0;
    }

    /*****
     * 计算折扣
     * @param price 价钱
     * @param salesDiscount 打折
     * @return 折扣之后的钱
     */
    public static BigDecimal discount (BigDecimal price ,String salesDiscount){
        return price.multiply(new BigDecimal(salesDiscount).multiply(new BigDecimal("0.10")));
    }

    /**
     * 提现订单sn
     *
     * @param 参数
     * @return 结果
     * @Author lx
     * @Date: 2022/07/08 10:24
     */
    public static String getPayNo() {
        String time = DateUtils.dateTime();
        String randomString = RandomStringUtils.randomNumeric(8);
        return "CASH"+time + randomString;
    }


    /**
     * 绑定支付账户 payNo
     *
     * @param 参数
     * @return 结果
     * @Author lx
     * @Date: 2022/07/08 10:24
     */
    public static String getInfoNo() {
        String time = DateUtils.dateTime();
        String randomString = RandomStringUtils.randomNumeric(8);
        return "INFO"+time + randomString;
    }

    /**
     * 获取短信验证码缓存key
     * @param type 验证码类型
     * @param phone 手机号
     * @return key
     */
    public static String getSmsCodeCacheKey(CitySmsTypeEnum type, String phone){
        return "sms:" + type.getType()+ phone ;
    }

    /**
     * 获取短信验证码是否重复获取缓存key
     * @param phone 手机号
     * @return key
     */
    public static String getSmsCodeCacheKey2(String phone){
        return "sms2:" + phone ;
    }
}
